package com.eims.sls_app.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.MD5;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.Code;
import com.eims.sls_app.utils.LotteryUtils;
import com.eims.sls_app.utils.NetWork;
import com.eims.sls_app.view.MyToast;

import org.json.JSONException;
import org.json.JSONObject;

public class ForgetPasswordActivity extends MyBaseActivity implements
		View.OnClickListener {
	private Context context = ForgetPasswordActivity.this;
	private ImageButton btn_back;
	private EditText forgetpassword_username, forgetpassword_checkma;
	private ImageView forgetpassword_checkma_show;
	private Button forgetpassword_ok;
	private String username;
	private String CheckCode;
	private MyAsynTask myAsynTask;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数
	private String mobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password);
		App.activityS.add(this);
		findView();
		init();
		setListener();
	}

	private void findView() {
		btn_back = (ImageButton) findViewById(R.id.btn_back);
		forgetpassword_username = (EditText) findViewById(R.id.forgetpassword_username);
		forgetpassword_checkma = (EditText) findViewById(R.id.forgetpassword_checkma);
		forgetpassword_checkma_show = (ImageView) findViewById(R.id.forgetpassword_checkma_show);
		forgetpassword_ok = (Button) findViewById(R.id.forgetpassword_ok);
	}

	private void init() {
		forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
				.createBitmap());
	}

	private void setListener() {

		btn_back.setOnClickListener(this);
		forgetpassword_checkma_show.setOnClickListener(this);
		forgetpassword_ok.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_forget_password, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.forgetpassword_checkma_show:
			forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
					.createBitmap());
			break;
		case R.id.forgetpassword_ok:
			submit();
			break;
		}
	}

	private void submit() {
		mobile = forgetpassword_username.getText().toString();
		CheckCode = forgetpassword_checkma.getText().toString();
		if (!LotteryUtils.checkPhoneREX(mobile)) {
			MyToast.getToast(this, "手机号码格式不正确").show();
			forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
					.createBitmap());
			return;
		}
		if (TextUtils.isEmpty(CheckCode)) {
			MyToast.getToast(this, "请输入验证码").show();
			forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
					.createBitmap());
			return;
		}

		if (!CheckCode.toLowerCase().equals(
				Code.getInstance().getCode().toLowerCase())) {
			MyToast.getToast(this, "验证码错误，请重新输入").show();
			forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
					.createBitmap());
			return;
		}

		if (NetWork.isConnect(this)) {
			myAsynTask = new MyAsynTask();
			myAsynTask.execute();

		} else {
			MyToast.getToast(this, "请检查网络").show();
		}

	}

	// 判断是否绑定了手机号码
	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		@Override
		protected String doInBackground(Integer... params) {
			String error = "-1";
			opt = "59";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(context);
			String key = MD5.md5(App.MD5_key);
			info = "{\"mobile\":\"" + mobile + "\"}";
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, "-1");
			auth = RspBodyBaseBean.getAuth(crc, time, imei, "-1");

			String values[] = { opt, auth, info };
			String result = HttpUtils.doPost(App.names, values,
					App.path);

			Log.i("x", "验证手机号info---------" + info);
			Log.i("x", "验证手机号result----" + result);

			if ("-500".equals(result))
				return result;
			try {
				JSONObject ob = new JSONObject(result);
				String hasPhoneNumber = ob.getString("hasPhoneNumber");
				if ("1".equals(hasPhoneNumber)) {
					return App.ERROR_SUCCESS + "";
				}
			} catch (JSONException e) {
				Log.w("TAG", "JSONException==>" + e.toString());
				error = "-1";
			}
			return error;

		}

		@Override
		protected void onPostExecute(String s) {
			super.onPostExecute(s);
			forgetpassword_checkma_show.setImageBitmap(Code.getInstance()
					.createBitmap());
			switch (Integer.valueOf(s)) {
			case 0:
				Intent intent = new Intent(context, ResetPasswordActivity.class);
				intent.putExtra("moblie", mobile);
				startActivity(intent);
				finish();
				break;
			case -1:
				MyToast.getToast(context, "该手机号码未绑定").show();
				break;
			}
		}
	}
}
